package com.qf.myblogssm.service.serviceImp;

import com.qf.myblogssm.dao.TarticleMapper;
import com.qf.myblogssm.dao.TtagMapper;
import com.qf.myblogssm.pojo.Tarticle;
import com.qf.myblogssm.pojo.Ttag;
import com.qf.myblogssm.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * create by 王镜 on 2019/6/17 0017 18:46
 */
@Service
public class TagServiceImpl implements TagService {
    @Autowired
    private TtagMapper ttagMapper;
    @Autowired
    private TarticleMapper tarticleMapper;

    @Override
    public List<Ttag> selectAllTags() {
        List<Ttag> ttags = ttagMapper.selectAll();

        return ttags;
    }

    @Override
    public Integer countTagNumber() {
        Integer tagnumber=ttagMapper.countTagNumber();
        return tagnumber;
    }

    @Override
    public List<Ttag> getTagsById(Integer id) {
        List<Ttag> ttags=ttagMapper.selectTagsById(id);
        return ttags;
    }

    @Override
    public Map<String, List<Tarticle>> getTagsByName() {
        List<Ttag> ttags=ttagMapper.selectTagsByName();

        Map<String,List<Tarticle>> map=new HashMap<>();
       for(Ttag ttag:ttags){
           List<Tarticle> tarticles=new ArrayList<>();
           List<Ttag> ttagList=ttagMapper.selectByName(ttag.getTag());
           for(Ttag ttag1:ttagList)
           {
               Tarticle tarticle= tarticleMapper.selectByPrimaryKey(ttag1.getId());
                tarticles.add(tarticle);
           }
           map.put(ttag.getTag(),tarticles);
       }
//        for(Tarticle tarticle:tarticles){
//            List<Tarticle> tarticleList= tarticleMapper.selectArticleBySort(tarticle.getSort());
//            //封装最后的数据
//            map.put(tarticle.getSort(),tarticleList);
//        }
        return map;
    }


}
